package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.CustomerUser;
import com.ruoyi.market.domain.Price;
import com.ruoyi.market.service.IPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 价格信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/price")
public class PriceController extends BaseController
{
    @Autowired
    private IPriceService priceService;

    /**
     * 获取价格列表
     */
    @PostMapping("/priceList")
    public TableDataInfo list(@RequestBody Price price, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            price.setShopId(shopId);
        };
        startPageByPost();
        List<Price> list = priceService.selectPriceList(price);
        return getDataTable(list);
    }

    /**
     * 根据价格编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:price:query')")
    @GetMapping(value = { "/", "/{priceId}" })
    public AjaxResult getInfo(@PathVariable(value = "priceId", required = false) Long priceId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(priceId))
        {
            Price sysPrice = priceService.selectPriceById(priceId);
            ajax.put(AjaxResult.DATA_TAG, sysPrice);
        }
        return ajax;
    }

    /**
     * 新增价格
     */
    @PreAuthorize("@ss.hasPermi('market:price:add')")
    @Log(title = "价格管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Price price, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            price.setShopId(shopId);
        };
        if (!priceService.checkPriceUnique(price))
        {
            return error("新增失败，已存在相同数据");
        }
        price.setCreateBy(getUsername());
        return toAjax(priceService.insertPrice(price));
    }

    /**
     * 修改价格
     */
    @PreAuthorize("@ss.hasPermi('market:price:edit')")
    @Log(title = "价格管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Price price)
    {
        if (!priceService.checkPriceUnique(price))
        {
            return error("修改失败，已存在相同数据");
        }
        price.setUpdateBy(getUsername());
        return toAjax(priceService.updatePrice(price));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:price:edit')")
    @Log(title = "价格管理", businessType = BusinessType.UPDATE)
    @PutMapping("/priceChangeStatus")
    public AjaxResult changeStatus(@RequestBody Price price)
    {
        price.setUpdateBy(getUsername());
        return toAjax(priceService.updatePriceStatus(price));
    }

    @Log(title = "价格管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('market:price:export')")
    @PostMapping("/priceExport")
    public void export(HttpServletResponse response, Price price)
    {
        List<Price> list = priceService.selectPriceList(price);
        ExcelUtil<Price> util = new ExcelUtil<Price>(Price.class);
        util.exportExcel(response, list, "价格数据");
    }

    @Log(title = "价格管理 - 导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('market:price:import')")
    @PostMapping("/priceImportData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Price> util = new ExcelUtil<Price>(Price.class);
        List<Price> priceList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = priceService.importPrice(priceList, updateSupport, operName);
        return success(message);
    }

    // 下载模板
    @PostMapping("/priceImportTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Price> util = new ExcelUtil<Price>(Price.class);
        util.importTemplateExcel(response, "价格数据");
    }

    /**
     * 删除价格
     */
    @PreAuthorize("@ss.hasPermi('market:price:remove')")
    @Log(title = "价格管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{priceIds}")
    public AjaxResult remove(@PathVariable Long[] priceIds)
    {
        return toAjax(priceService.deletePriceByIds(priceIds));
    }
}
